using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.TestDrivens;
using Pixysoft.Framework.Configurations;
using Pixysoft.Framework.Configurations.Core;

namespace Pixysoft.Framework.Configurations.TestCases
{
    [TestDriven(TestDrivenType.IntegrationTest, "Configuration")]
    class UnitTest_042_Configuration
    {
        static IAssert assert = Assert.New(true);

        [TestStart]
        public static bool TestCaseStart()
        {

            return true;
        }

        [TestStop]
        public static void TestCaseStop()
        {
        }

        [TestStateSave]
        public static ITestDrivenDocument TestCaseStateSave()
        {
            return assert.Document;
        }

        [TestCase("Import")]
        public static void TestCase_000_Import()
        {
            Assert.Call(UnitTest_020_ConfigurationSetting.TestCase_000_Save);


            foreach (IConfiguration config in Configuration.Import())
            {
                assert.Write(config.Id);
            }

            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string filename = StringHelper.default_config_filename;



        }

        [TestCase("Import")]
        public static void TestCase_001_Import()
        {


            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string filename = StringHelper.default_config_filename;

            foreach (IConfiguration config in Configuration.Import(filename))
            {
                assert.Write(config.Id);
            }




        }

        [TestCase("Import")]
        public static void TestCase_002_Import()
        {


            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string filename = StringHelper.default_config_filename;

            foreach (IConfiguration config in Configuration.Import(path, filename))
            {
                assert.Write(config.Id);
            }

            foreach (IConfiguration config in Configuration.Import(path, filename))
            {
                Configuration.Import(path, "abc.config");
            }
        }

        [TestCase("Deserialize")]
        public static void TestCase_004_Deserialize()
        {


            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string filename = StringHelper.default_config_filename;

            string id = "DemoConfiguration";
            string name = "name";

            ConfigurationSetting setting = new ConfigurationSetting(id, name);
            string nodename = "nodename";
            IConfigurationNode node1 = new ConfigurationNode(nodename);
            node1.Text = "text";
            node1.Property.Add("property1", "property1");
            IConfigurationProperty property2 = node1.Property.Add("property2");

            string dllname = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory) + "Pixysoft.dll";
            string classname = "Pixysoft.ByteBuilder";
            node1.Property.Add(StringHelper.default_attribute_filename, dllname);
            node1.Property.Add(StringHelper.default_attribute_classname, classname);
            setting.Node.Add(node1);

            IConfigurationNode node2 = new ConfigurationNode(nodename);
            setting.Nodes.Add(node2);

            setting.Save();

            IConfiguration config = Configuration.GetConfiguration(id, name);
            assert.Write(config.Id);

            assert.IsNull(Configuration.GetConfiguration(id));

            string value = setting.Serialize();

            config = Configuration.Deserialize(value, id, name);
            assert.Write(config.Id);


            DemoConfiguration dconfig = Configuration.Deserialize<DemoConfiguration>(setting.Serialize(), id, name);
            assert.IsNotNull(dconfig);
            assert.IsInstanceOfType(typeof(DemoConfiguration), dconfig);
            assert.IsNotNull(dconfig.nodename);

            IConfiguration config3 = Configuration.Default;
            assert.IsNotNull(config3);
            assert.Write(config3.Id);
        }

    }

    public class DemoConfiguration
    {
        DemoNode _nodename;
        public DemoNode nodename
        {
            get
            {
                return _nodename;
            }
            set
            {
                this._nodename = value;
            }
        }
    }

    public class DemoNode
    {
    }
}
